package fr.web325.servlet.admin.notice;

import fr.web325.bean.Notice;
import fr.web325.dao.NoticeDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

    @WebServlet("/shop/admin/notice")
public class NoticeList extends HttpServlet {
    public static final String DEFAULT_FORMAT = "yyyyMMddHHmmss";// 时间格式
    private static SimpleDateFormat sfEnd   = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Notice> noticeList = new NoticeDao().getNoticeList();
            SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
            try {
                for (int i = 0; i < noticeList.size(); i++) {
                    noticeList.get(i).setN_time(sfEnd.format(sdf.parse(noticeList.get(1).getN_time())));
                }
            } catch (ParseException e) {
                System.out.println(e.getMessage());
            }
            req.setAttribute("noticeList",noticeList);
            req.getRequestDispatcher("notice/index.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
